package client.components;

import java.util.Date;
import java.util.Map;

import client.bean.Member;
import client.bean.Plan;

import com.chap.links.client.Timeline;
import com.google.gwt.visualization.client.DataTable;

public class PlanTimeline extends Timeline {

	public PlanTimeline(Plan plan) {
		super(getData(plan), getOption(plan));
	}

	private static Timeline.Options getOption(Plan plan) {
		Timeline.Options option = prepareTimeOption();
		option.setMin(plan.getStartTime());
		option.setMax(plan.getEndTime());
		return option;
	}

	private static DataTable getData(Plan plan) {
		DataTable data = prepareDataTable();
		for (Map.Entry<Member, Boolean> entry : plan.getMembers().entrySet()) {
			Member member = entry.getKey();
			if (member.getConfirmedDates() != null) {
				for (Map.Entry<Date, Date> dates : member.getConfirmedDates().entrySet()) {
					data.addRow();
					data.setValue(0, 0, dates.getKey());
					data.setValue(0, 1, dates.getValue());
					data.setValue(0, 2, member.getName());
				}
			}
		}
		return data;
	}

	private static Timeline.Options prepareTimeOption() {
		Timeline.Options options = Timeline.Options.create();
		options.setWidth("100%");
		options.setHeight("200px");
		options.setStyle(Timeline.Options.STYLE.BOX);
		// options.setEditable(true);
		return options;
	}

	private static DataTable prepareDataTable() {
		DataTable data = DataTable.create();
		data.addColumn(DataTable.ColumnType.DATETIME, "startdate");
		data.addColumn(DataTable.ColumnType.DATETIME, "enddate");
		data.addColumn(DataTable.ColumnType.STRING, "content");
		return data;
	}
}
